package com.example.manager.service.Imple;

import com.example.manager.entity.*;
import com.example.manager.mapper.BookMapper;
import com.example.manager.service.IBookService;
import com.example.manager.utils.borrowPageBean;
import com.example.manager.utils.followPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookService implements IBookService {

    @Autowired
    private BookMapper bookMapper;

//    获取总书籍数量
    @Override
    public Integer getAllBookSize() {

        int total = 0;
        total = bookMapper.getAllBookSize();

        return total;

    }

//    分页
    @Override
    public PageBean getBookByPage(Integer currentpage, Integer pageSize) {
        PageBean pageBean = new PageBean();
        List<Book> books = bookMapper.getBookByPage((currentpage -1)*5,pageSize);
        int totalSize = bookMapper.getAllBookSize();
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(currentpage);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setBooks(books);


        return pageBean;

    }

//    搜索图书
    @Override
    public PageBean findBook(String search, Integer pageSize) {
        PageBean pageBean = new PageBean();
        List<Book>books = bookMapper.findBook(search);
        int totalSize = bookMapper.getAllBookSize1(search);
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(1);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setBooks(books);

        return pageBean;
    }

    //    删除图书
    @Override
    public void delBook(Integer bookid) {
        bookMapper.delBook(bookid);
        return ;
    }


//    查找图书
    @Override
    public Book findBookById(int bookid) {
        Book book = new Book();
        book = bookMapper.findById(bookid);

        return book;
    }


//    添加图书
    @Override
    public Integer addBook(Book book) {
        bookMapper.insert(book);
        return null;
    }


//    编辑图书信息
    @Override
    public void editBook(Book book) {
        bookMapper.updatebook(book);

        return ;
    }
//      借书
    @Override
    public void borrowBook(Borrow borrow) {
        bookMapper.borrowBook(borrow);
        bookMapper.insertborrow(borrow);
    }
//      借书页面分页
    @Override
    public borrowPageBean getBorrowBooks(Integer username, Integer currentpage, Integer pageSize) {
        borrowPageBean pageBean = new borrowPageBean();
        List<Book> books = bookMapper.getBorrowBooks(username,(currentpage -1)*5,pageSize);
        int totalSize = bookMapper.getAllBorrowSize(username);
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(currentpage);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setBooks(books);


        return pageBean;

    }

//    还书
    @Override
    public void backBook(Integer bookid) {
        bookMapper.backbook(bookid);
        bookMapper.backbook1(bookid);
    }

//    管理员借书管理页面分页
    @Override
    public borrowPageBean getBorrowBooks1(Integer currentpage, Integer pageSize) {
        borrowPageBean pageBean = new borrowPageBean();
        List<Book> books = bookMapper.getBorrowBooks1((currentpage -1)*5,pageSize);
        int totalSize = bookMapper.getAllBorrowSize1();
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(currentpage);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setBooks(books);


        return pageBean;
    }

//    收藏页面分页
    @Override
    public followPageBean getFollowBooks(Integer username, Integer currentpage, Integer pageSize) {
        followPageBean pageBean = new followPageBean();
        List<Book> books = bookMapper.getFollowBooks(username,(currentpage -1)*5,pageSize);
        int totalSize = bookMapper.getAllFollowSize(username);
        pageBean.setTotalSize(totalSize);
        pageBean.setCurrentPage(currentpage);
        pageBean.setTotalPage(totalSize % pageSize == 0 ? totalSize / pageSize : (totalSize / pageSize + 1));
        pageBean.setBooks(books);


        return pageBean;    }

//    收藏图书
    @Override
    public void followBook(Follow follow) {
        bookMapper.followBook(follow);
    }

//    取消收藏
    @Override
    public void delFollowBook(Integer bookid) {
        bookMapper.delFollowBook(bookid);
    }

    @Override
    public List checkFollowBooks(Integer username) {
        List list = new ArrayList();
        list = bookMapper.checkFollowBook(username);
        return list;
    }
}
